package com.itheima.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * @author: libin
 * @date: 2019/11/28 10:46
 * @version: 1.0
 * @describe:
 */
public class SmsUtil {
    /**
     *
     * @param mobile
     * @param code 短信验证码
     */
    public static void sendSmsCode(String mobile,String code){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "LTAI4FmwpSzanyrajankHLDZ", //替换成你的
                "yUv6ohhrzrfV1RIKZ9yfaTLXNgy7pw");//替换成你的
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("SignName", "体检预约"); //替换成你的签名
        request.putQueryParameter("TemplateCode", "SMS_178755624");//替换成你的模板

        request.putQueryParameter("PhoneNumbers", mobile);
        JSONObject json = new JSONObject();
        json.put("code",code);
        request.putQueryParameter("TemplateParam", json.toJSONString());
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        sendSmsCode("17706214292","464652");
    }
}

